﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WareHouse.App
{
    using WareHouse.Contract.Models;
    using WareHouse.Contract.Services;
    using WareHouse.Service.Services;

    public partial class FrmCustomerDebtRate : Form
    {
        private CustomerDebtRateModel model = new CustomerDebtRateModel();

        private IWareHouseService wareHouseService;
        public FrmCustomerDebtRate()
        {
            wareHouseService=new WareHouseService();
            InitializeComponent();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void UpdateModel()
        {
            model.CustomerId = int.Parse(ddlCustomer.EditValue.ToString());
            model.Year = int.Parse(ddlYear.SelectedItem.ToString());
            model.Month1 = float.Parse(txtT1.Text != "" ? txtT1.Text : "0");
            model.Month2 = float.Parse(txtT2.Text != "" ? txtT2.Text : "0");
            model.Month3 = float.Parse(txtT3.Text != "" ? txtT3.Text : "0");
            model.Month4 = float.Parse(txtT4.Text != "" ? txtT4.Text : "0");
            model.Month5 = float.Parse(txtT5.Text != "" ? txtT5.Text : "0");
            model.Month6 = float.Parse(txtT6.Text != "" ? txtT6.Text : "0");
            model.Month7 = float.Parse(txtT7.Text != "" ? txtT7.Text : "0");
            model.Month8 = float.Parse(txtT8.Text != "" ? txtT8.Text : "0");
            model.Month9 = float.Parse(txtT9.Text != "" ? txtT9.Text : "0");
            model.Month10 = float.Parse(txtT10.Text != "" ? txtT10.Text : "0");
            model.Month11 = float.Parse(txtT11.Text != "" ? txtT11.Text : "0");
            model.Month12 = float.Parse(txtT12.Text != "" ? txtT12.Text : "0");
            model.UpdateBy = General.userName;
            model.LastUpdate = DateTime.Now;

        }


        private void LoadDataGrid()
        {
            this.dtgCustomerDebtRate.DataSource = this.wareHouseService.ListCustomerDebtRate();
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {





            if (btnAdd.Text == "THÊM MỚI")
            {
                General.ChangeButtonStatus("insert", btnAdd, btnUpdate, btnDelete);

                txtT1.Text = "0";
                txtT2.Text = "0";
                txtT3.Text = "0";
                txtT4.Text = "0";
                txtT5.Text = "0";
                txtT6.Text = "0";
                txtT7.Text = "0";
                txtT8.Text = "0";
                txtT9.Text = "0";
                txtT10.Text = "0";
                txtT11.Text = "0";
                txtT12.Text = "0";

                txtDateCreate.Text = "";

                txtLastUpdate.Text = "";
                txtUserCreate.Text = "";
                txtUserUpdate.Text = "";

                return;
            }
            if (btnAdd.Text == "LƯU")
            {


                if (ddlCustomer.EditValue == null || ddlCustomer.Text =="")
                {
                    MessageBox.Show("Tên Khách hàng không thể bỏ trống");
                    return;
                }
                UpdateModel();
                model.EffectiveFromDate = DateTime.Now;
                model.DateCreate = DateTime.Now;
     
                model.UserCreate = General.userName;
                var result = this.wareHouseService.SaveOrUpdateCustomerDebtRate(this.model);
                if (result != null)
                {

                    MessageBox.Show("Thêm mới thành công", "Thông báo");

                    General.ChangeButtonStatus("insert", this.btnAdd, this.btnUpdate, this.btnDelete);
                    this.LoadDataGrid();
                }
            }
            else
                MessageBox.Show("Thêm mới thất bại vui lòng kiểm tra lại dữ liệu ", "Thông báo");


        }

        private void btnDelete_Click(object sender, EventArgs e)
        {


            if (btnDelete.Text == "HỦY")
            {
                General.ChangeButtonStatus("update", btnAdd, btnUpdate, btnDelete);

            }
            else if (btnDelete.Text == "XÓA")
            {
                if (MessageBox.Show(General.delConfirmInfo, General.delConfirmCaption, MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    this.UpdateModel();
                    if (model.CustomerId > 0)
                    {
                        var result = wareHouseService.DeleteCustomerDebtRate(model);
                        if (result)
                        {
                            General.ChangeButtonStatus("delete", btnAdd, btnUpdate, btnDelete);
                            MessageBox.Show("Xóa thành công", "Thông báo");
                            this.LoadDataGrid();
                            return;
                        }
                        MessageBox.Show("Xóa thất bại", "Thông báo");

                    }
                }
            }
            else return;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {

            if (btnUpdate.Text == "CẬP NHẬT")
            {
                General.ChangeButtonStatus("update", btnAdd, btnUpdate, btnDelete);

                return;

            }
            else
                if (btnUpdate.Text == "HỦY")
                {
                    General.ChangeButtonStatus("insert", btnAdd, btnUpdate, btnDelete);


                }
                else if (btnUpdate.Text == "LƯU")
                {
                    if (ddlCustomer.EditValue == null || ddlCustomer.Text == "")
                    {
                        MessageBox.Show("Tên Khách hàng không thể bỏ trống");
                        return;
                    }

                    UpdateModel();
                    var result = wareHouseService.SaveOrUpdateCustomerDebtRate(model);


                    if (result != null)
                    {
                        MessageBox.Show("Cập nhật thành công", "Thông báo");
                        this.LoadDataGrid();
                        General.ChangeButtonStatus("update", btnAdd, btnUpdate, btnDelete);

                    }
                }
                else
                    MessageBox.Show("Cập nhật thất bại vui lòng kiểm tra lại dữ liệu ", "Thông báo");
        }

        private void FrmCustomerDebtRate_Load(object sender, EventArgs e)
        {
            CheckRole();
            this.LoadDataGrid();
            LoadDataCustomer();
            ImportDataYear();
        }

        private void ImportDataYear()
        {
            for (int i = DateTime.Now.Year+10; i > DateTime.Now.Year-10; i--)
            {
                ddlYear.Items.Add(i);
            }
        }

        private void LoadDataCustomer()
        {
            ddlCustomer.Properties.DataSource = wareHouseService.ListCustomer();
            ddlCustomer.Properties.ValueMember = "CustomerId";
            ddlCustomer.Properties.DisplayMember = "FullName";
        }

        private void dtgCustomerDebtRate_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                model = (CustomerDebtRateModel)dtgCustomerDebtRate.SelectedRows[0].DataBoundItem;


                txtT1.Text = model.Month1.ToString();
                txtT2.Text = model.Month2.ToString();
                txtT3.Text = model.Month3.ToString();
                txtT4.Text = model.Month4.ToString();
                txtT5.Text = model.Month5.ToString();
                txtT6.Text = model.Month6.ToString();
                txtT7.Text = model.Month7.ToString();
                txtT8.Text = model.Month8.ToString();
                txtT9.Text = model.Month9.ToString();
                txtT10.Text = model.Month10.ToString();
                txtT11.Text = model.Month11.ToString();
                txtT12.Text = model.Month12.ToString();
                ddlYear.SelectedItem = model.Year;
                ddlCustomer.EditValue = model.CustomerId;

                txtDateCreate.Text = model.DateCreate != null
                                         ? string.Format(
                                             "{0:dd-MM-yyyy}",
                                             model.LastUpdate)
                                         : "";

                txtLastUpdate.Text = model.LastUpdate != null ? string.Format(
                                             "{0:dd-MM-yyyy}",
                                            model.LastUpdate) :
                "";
                txtUserCreate.Text = this.model.UserCreate ?? "";
                txtUserUpdate.Text = this.model.UpdateBy ?? "";
            }
            catch (Exception)
            {

                return;
            }
        }

        private void txtT1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
            {
                e.Handled = true;
            }

        }

        private void CheckRole()
        {
            if (!wareHouseService.R(General.groupUserId, "customerdebtrate_add"))
            {
                btnAdd.Enabled = false;

            }
            if (!wareHouseService.R(General.groupUserId, "customerdebtrate_edit"))
            {
                btnUpdate.Enabled = false;

            }
            if (!wareHouseService.R(General.groupUserId, "customerdebtrate_delete"))
            {
                btnDelete.Enabled = false;

            }
            
             

        }
    }
}
